/*
 * AgendaMultiController.java
 *
 * Created on December 29, 2008, 6:03 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.vacuela.publishmx.web.controllers;
import java.util.Arrays;
import java.util.List;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;
import org.vacuela.publishmx.constant.Constants;
import org.vacuela.publishmx.context.ServiceContext;
import org.vacuela.publishmx.context.ServiceContextImpl;
import org.vacuela.publishmx.domains.Contact;
import org.vacuela.publishmx.domains.Group;
import org.vacuela.publishmx.services.ContactBookService;
import org.vacuela.publishmx.util.PublishUtil;
import org.vacuela.publishmx.web.bean.UserSession;

/**
 *
 * @author nestor.carrasco
 */
public class AgendaMultiController extends MultiActionController{
    private ContactBookService bookSvc;
    
    public ModelAndView showMyAgenda(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
        
        ServiceContext context = new ServiceContextImpl();
        UserSession userSession = (UserSession)WebUtils.getRequiredSessionAttribute(request,Constants.USER_SESSION_DATA);
        context.setAttribute("userId", userSession.getUserDomain().getId());
        this.bookSvc.getGroupsByUserId(context);
        List<Group> myGroupsList  = (List<Group>)context.getAttribute("groupList");
        //String msg = "message is Here";
        ModelAndView mav = new ModelAndView("contact/showAgenda");
        //mav.addObject("message", msg);
        mav.addObject("groupList", myGroupsList);
        return mav;
        
    }
    
    public ModelAndView getContactsByGroupId(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
        ServiceContext context = new ServiceContextImpl();
        String groupId = PublishUtil.checkNull(request.getParameter("groupAction"),"");
        String contactIdSel = PublishUtil.checkNull(request.getParameter("contactIdSel"),"");
        
        context.setAttribute("groupId",groupId);
        bookSvc.getContactByGroupId(context);
        List<Contact> lstContact = ( List<Contact> )context.getAttribute("lstContact");
        ModelAndView mav = new ModelAndView("contact/plainContactsList");
        mav.addObject("lstContact",lstContact);
        
        if (!"".equals(contactIdSel)){
            String[] lstContactId = StringUtils.split(contactIdSel,',');
            List arrayToList = Arrays.asList(lstContactId);
            mav.addObject("lstContactSelected",arrayToList);
        }
        return mav;
    }
    
    public ModelAndView contactsSelectedView(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
        String[] contactIdSel = request.getParameterValues("chkContact");
        ModelAndView mav = new ModelAndView("contact/showAgenda");
        if (null != contactIdSel){
            mav.addObject("listContactsSlected",StringUtils.join(contactIdSel,','));
            mav.addObject("totalContactsSelected",contactIdSel.length);
        }else{
            mav.addObject("listContactsSlected","");
            mav.addObject("totalContactsSelected","0");
        }
        mav.addObject("flagInst","SET_IDS_CLOSE");
        return mav;
    }
    public void setContactBookService(ContactBookService bookSvc) {
        this.bookSvc = bookSvc;
    }
}
